package com.jd.seg;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

import com.jd.seg.util.Util;

public class MMSeg implements ISegmenter {

	@Override
	public SegResult seg(String str) {
		SegResult segResult = null;
		if (StringUtils.isNotEmpty(str)){
			char [] chars = str.toCharArray();
			int start = 0;
			boolean isEnd = false;
			segResult = new SegResult();
			List<Word> wordList = new ArrayList<Word>();
			List<Word> unLogingWordList = new ArrayList<Word>();
			List<String> words = new ArrayList<String>();
			while(!isEnd){
				int maxLen = DictUtil.getMaxLenStartBy(new String(ArrayUtils.subarray(chars, start, start+1)));
				if(maxLen == 0){
					maxLen = 1;
				}
				for(int len=maxLen;len>0;len--){//每次不匹配，指针向前移动。
					int end = start+len;
					char [] tmpChars = ArrayUtils.subarray(chars, start, end);
					String word = Util.toString(tmpChars);
					if(DictUtil.match(word)){
						Word mWords = new Word();
						mWords.setContent(word);
						mWords.setDomain(null);
						mWords.setFrequency(0);
						mWords.setSort("");
						wordList.add(mWords);
						start += len;
						if (start>chars.length){
							isEnd = true;
						}
						break;
					}else{

						if(len==1){
							if(CharUtils.isAsciiAlphanumeric(chars[start])){
								boolean isAlphaNumric = true;
								int posi = start;
								while(isAlphaNumric){
									posi += 1;
									if(posi==chars.length || !CharUtils.isAsciiAlphanumeric(chars[posi])){
										isAlphaNumric = false;
										break;
									}
								}
								word = Util.toString(ArrayUtils.subarray(chars, start, posi));
								if(StringUtils.isNumeric(word)){
									int unitLen = 4;
									if((posi+unitLen)>chars.length){
										unitLen = chars.length-posi;
									}
									for(int i=unitLen;i>0;i--){
										
										String unit = new String(ArrayUtils.subarray(chars, posi, posi+i));
										if(DictUtil.inUnitsDic(unit)){
											posi += i;
											word = Util.toString(ArrayUtils.subarray(chars, start, posi));
											break;
										}
									}
								}
								start = posi;
								
							}else{
								start += len;
							}
							Word mWord = new Word();
							mWord.setContent(word);
							mWord.setDomain(null);
							mWord.setFrequency(0);
							mWord.setSort("");
							if(!DictUtil.match(word)){
								unLogingWordList.add(mWord);
							}
							wordList.add(mWord);
							
						}
						
						if (start==chars.length){
							isEnd = true;
							break;
						}
					}
				}
				
			}
			segResult.setNotLonginWords(unLogingWordList);
			segResult.setWords(wordList);
		}
		
		return segResult;
	}
	
	public static void main(String[] args) {
		MMSeg seg  = new MMSeg();
		seg.seg("买来送朋友的。下单时就和客服确认，我要5天内一定收到。否则我就退单，买电子礼品卡了。客服说如果5天后还没有收到可以退单。第五天，未到货，客服说还在路上，什么时候能到不知道。我说那我撤单买电子的吧。答复：不能退单。失望！");
			
		for(Word word:seg.seg("第一次买礼品卡，卡面还挺漂亮的，用来送人的。可以开发票，很方便，下次还会来。").getWords())
			System.out.println(word.getContent());
	}

}
